using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Xsl;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;

namespace Code5Systems.FormMaster
{

	public partial class FormAutoRespMsg : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		
#region  Web Form Designer Generated Code
		
		//This call is required by the Web Form Designer.
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		
		protected DotNetNuke.UI.UserControls.TextEditor htmlEditor;
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.Xml xmlTokens;
		
		//NOTE: The following placeholder declaration is required by the Web Form Designer.
		//Do not delete or move it.
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: This method call is required by the Web Form Designer
			//Do not modify it using the code editor.
			InitializeComponent();
		}
		
#endregion
		
		
#region Private Members
		
		private DotNetNuke.Entities.Modules.PortalModuleBase _loaderCtl;
		private Code5Systems.FormMaster.Business.FormInfo objFormInfo;
		//Private objFormXmlDoc As XmlDocument
		private XDocument xDoc;
		private Code5Systems.FormMaster.Business.FormController objFormCtl;
		private FormSettings objFormSettings;
		private string strOperationSuccess;
		
#endregion
		
		
#region Event Handlers
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			try
			{
				this.LoaderCtl = (PortalModuleBase) this.Parent.Parent;
				
				strOperationSuccess = Localization.GetString("OperationSuccess", this.LocalResourceFile);
				
				objFormSettings = Utility.GetFormSettings(ModuleId);
				objFormCtl = new Code5Systems.FormMaster.Business.FormController(ModuleId);
				objFormInfo = objFormCtl.FormInfo;
				
				xDoc = XDocument.Parse(objFormInfo.FormDefinition);
				
				BindTokens();
				
				if (!Page.IsPostBack)
				{
					BindData();
				}
			}
			catch (Exception exc)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, exc.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
			}
		}
		
		protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
		{
			XElement xAutoResp = default(XElement);
			
			try
			{
				xAutoResp = xDoc.Root.Element("AutoResponse");
				
				if (xAutoResp == null)
				{
					xAutoResp = new XElement("AutoResponse");
					xDoc.Root.Add(xAutoResp);
				}
				
				if (xAutoResp != null)
				{
					xAutoResp.RemoveAll();
					xAutoResp.Add(new XCData(htmlEditor.Text));
					objFormCtl.UpdateFormDefinition(ModuleId, xDoc.ToString(), this.UserId);
				}
				
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
				
			}
			catch (Exception exc)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, exc.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
			}
		}
#endregion
		
		
#region Data Bind Methods
		
		private void BindData()
		{
			XElement xAutoResp = default(XElement);
			
			xAutoResp = xDoc.Root.Element("AutoResponse");
			
			if (xAutoResp != null)
			{
				htmlEditor.Text = Server.HtmlDecode(xAutoResp.Value);
			}
			
		}
		
		private void BindTokens()
		{
			XsltArgumentList xslArg = new XsltArgumentList();
			
			XDocument xPageDoc = default(XDocument);
			XDocument xDoc = XDocument.Parse((string) (objFormCtl.Pages[0].PageData));
			XElement xParentControls = xDoc.Root.Elements("Controls").ElementAtOrDefault(0);
			foreach (Code5Systems.FormMaster.Business.FormPageInfo page in objFormCtl.Pages)
			{
				if (page.PageNum > 1)
				{
					xPageDoc = XDocument.Parse(page.PageData);
					IEnumerable<XElement> controls = xPageDoc.Root.Elements("Controls").Elements("Control");
					foreach (XElement xControl in controls)
					{
						xParentControls.Add(xControl);
					}
				}
			}
			
			TextReader reader = new StringReader(xDoc.ToString());
			XPathDocument xPathDoc = new XPathDocument(reader);
			
			xmlTokens.XPathNavigator = xPathDoc.CreateNavigator();
			xmlTokens.TransformArgumentList = xslArg;
			xmlTokens.TransformSource = "assets/xslt/tokens.xslt";
			
		}
		
#endregion
		
		
#region Public Properties
		public PortalModuleBase LoaderCtl
		{
			get
			{
				if (_loaderCtl == null)
				{
					_loaderCtl = this;
				}
				
				return _loaderCtl;
			}
			set
			{
				_loaderCtl = value;
			}
		}
#endregion
		
		
	}
	
}


